import { api, apiBlob } from '@/api'
import useUserStore from '@/store/modules/user'
import { reject } from 'lodash'

const useFileStore = defineStore(
    // 唯一ID
    'file',
    {
        state: () => ({

            permissions: []
        }),
        getters: {

        },
        actions: {
            // 删除文件
            deleteFile(fileId) {
                return new Promise((resolve, reject) => {
                    api.delete(`files/${fileId}`, {
                    }).then(res => {
                        resolve(res)
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            // 获取文件列表
            getFileList(parentFileId) {
                return new Promise((resolve, reject) => {
                    api.get(`files/${parentFileId}`, {
                    }).then(res => {
                        resolve(res)
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            // --根据路径获取个人文件列表
            // 根据路径获取文件列表
            getFileListByPath(params) {
                return new Promise((resolve, reject) => {
                    api.get('files', { params }, {
                    }).then(res => {
                        resolve(res)
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            // 移动文件
            moveFile(data) {
                return new Promise((resolve, reject) => {
                    // 通过 mock 进行登录
                    api.put('files', data, {
                    }).then(
                        () => {
                            resolve()
                        }).catch(error => {
                        reject(error)
                    })
                })
            },
            // -----废弃 下载媒体文件
            downloadMedia(fileId) {
                return new Promise((resolve, reject) => {
                    api.get(`files/media/${fileId}`, {
                    }, { responseType: 'blob' }).then(res => {
                        // let buffer = new Buffer(res, 'binary')

                        resolve(res)
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            // 获取媒体文件流接口
            getMediaStream(fileId) {
                const userStore = useUserStore()
                return new Promise((resolve, reject) => {
                    apiBlob.get(`files/${fileId}/media-stream/${userStore.token}`, {
                    }).then(res => {
                        resolve(res)
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            getMediaStreamApi(fileId) {
                const userStore = useUserStore()
                return new Promise((resolve, reject) => {
                    api.get(`files/${fileId}/media-stream/${userStore.token}`, {
                    }).then(res => {
                        resolve(res)
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            // 获取文件内容
            getContent(fileId) {
                return new Promise((resolve, reject) => {
                    api.get(`files/${fileId}/content`, {
                    }).then(res => {
                        resolve(res)
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            // 下载文件
            download(params) {
                const userStore = useUserStore()

                return new Promise((resolve, reject) => {
                    apiBlob.get(`files/${params.fileIds}/download/${userStore.token}`,
                        {
                        }
                    ).then(res => {
                        resolve(res)
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            // 获取共享文件列表
            getPublicFileList() {
                return new Promise((resolve, reject) => {
                    api.get('files/share', {

                    }).then(res => {
                        resolve(res)
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            // 获取个人文件列表
            getPersonalFileList() {
                return new Promise((resolve, reject) => {
                    api.get('files/personal', {
                    }).then(res => {
                        resolve(res)
                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            // 上传文件
            updateFile(data) {

                return new Promise(resolve => {
                    api.post(`files/${data.parentId}/upload`, {
                        file: data.file
                    }, {
                        // baseURL: '/mock/'
                    }).then(() => {

                        resolve()
                    })
                })
            },
            // 创建目录
            create(data) {

                return new Promise((resolve, reject) => {
                    // 通过 mock 进行登录
                    api.post('files', data, {
                        // baseURL: '/mock/'
                    }).then(
                        resolve()
                    ).catch(error => {
                        reject(error)
                    })
                })
            },
            // 创建文件
            createFile(data) {
                return new Promise((resolve, reject) => {
                    // 通过 mock 进行登录
                    api.post('files/file', data, {
                    }).then(
                        resolve()
                    ).catch(error => {
                        reject(error)
                    })
                })
            },
            // 续传文件
            dotUpload(data) {
                return new Promise((resolve, reject) => {
                    // 通过 mock 进行登录
                    api.put(`files/${data.parentId}/write`, {
                        file: data.file
                    }, {
                    }).then(
                        resolve()
                    ).catch(error => {
                        reject(error)
                    })
                })
            },
            // 修改文件信息
            editFile(params) {
                return new Promise(resolve => {
                    // 通过 mock 获取权限
                    api.put(`files/${params.fileId}`, {
                        // baseURL: '/mock/',
                        name: params.name,
                        parentId: params.parentId,
                        tagIds: params.tagIds,
                        share: params.share
                    }).then(
                        resolve()
                    )
                })
            },
            // 写入文件文本信息
            writeDoc(data) {
                return new Promise((resolve, reject) => {
                    // 通过 mock 进行登录
                    api.put(`files/${data.fileId}/write`, {
                        content: data.content
                    }, {
                    }).then(
                        resolve()
                    ).catch(error => {
                        reject(error)
                    })
                })
            },
            // 查看权限组列表
            permissonGroups() {
                return new Promise((resolve, reject) => {
                    api.get(`permission-groups`, {}).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 新增权限组
            permissonGroupsAdd(data) {
                return new Promise((resolve, reject) => {
                    api.post(`permission-groups`, data).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 删除权限组
            permmissonGroupsDelete(id) {
                return new Promise((resolve, reject) => {
                    api.delete(`permission-groups/` + id, ).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 查看权限组的成员ID数组
            permmissonGroupsFileId(id) {
                return new Promise((resolve, reject) => {
                    api.get(`permission-groups/` + id + '/users' ).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 查看权限组的成员数
            permmissonGroupsUserTree(id) {
                return new Promise((resolve, reject) => {
                    api.get(`permission-groups/${id}/user-tree`, ).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 修改权限组的成员
            permmissonGroupsPutUser(id, data) {
                return new Promise((resolve, reject) => {
                    api.put(`permission-groups/${id}/users`, data).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 修改权限组的文件权限
            permmissonGroupsPutFile(id, data) {
                return new Promise((resolve, reject) => {
                    api.put(`permission-groups/${id}/permissions`, data).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 查看权限组的文件权限信息（共享存储根目录）
            permmissonGroupsFiles(id) {
                return new Promise((resolve, reject) => {
                    api.get(`permission-groups/${id}/files`, ).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 查看权限组的文件权限信息
            permmissonGroupsFilesFileId(id, fileId) {
                return new Promise((resolve, reject) => {
                    api.get(`permission-groups/${id}/files/${fileId}`, ).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 获取标签文件
            tagsGet() {
                return new Promise((resolve, reject) => {
                    api.get(`tags`, ).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 获取文件列表根据标签
            tagsGetByFiles(id) {
                return new Promise((resolve, reject) => {
                    api.get(`tags/${id}/files`, ).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 创建新标签
            createTags(data) {
                return new Promise((resolve, reject) => {
                    api.post(`tags`, data).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 移除标签
            removeTags(id, data) {
                return new Promise((resolve, reject) => {
                    api.put(`tags/${id}/remove-from-files`, data).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 添加标签到文件
            addTagsByFiles(id, data) {
                return new Promise((resolve, reject) => {
                    api.put(`tags/${id}/add-to-files`, data).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 删除标签
            deleteTags(id) {
                return new Promise((resolve, reject) => {
                    api.delete(`tags/${id}`).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 创建文件
            createArticle(data) {
                return new Promise((resolve, reject) => {
                    api.post(`files/file`, data).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 写入文件信息
            writeArticle(id, data) {
                return new Promise((resolve, reject) => {
                    api.put(`files/${id}/write`, data).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 查询审批记录
            approvalGet(params) {
                return new Promise((resolve, reject) => {
                    api.get(`approval-records`, {params}, {} ).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 创建审批
            approvalCreate(data) {
                return new Promise((resolve, reject) => {
                    api.post(`approval-records`, data ).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 审批
            approvalTake(id, data) {
                return new Promise((resolve, reject) => {
                    api.put(`approval-nodes/${id}`, data ).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            filePath(path) {
                return new Promise((resolve, reject) => {
                    api.get(`approvals/files/${path}`).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 根据标签获取目标文件夹下的视频文件列表
            shareVideos(fileId, tagId) {
                return new Promise((resolve, reject) => {
                    api.get(`/files/public-share/${fileId}/videos?tagId=${tagId}`).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 获取共享文件夹下所有子文件的标签列表
            shareTags(id) {
                return new Promise((resolve, reject) => {
                    api.get(`/files/public-share/${id}/tags`).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 共享文件的流媒体窗口
            shareMedia(id, params) {
                return new Promise((resolve, reject) => {
                    api.get(`/files/public-share/${id}/media-stream?userName=${params.userName}&areaName=${params.areaName}&bedID=${params.bedID}`).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 分页查询舆情内容
            onlineContent(page, size) {
                return new Promise((resolve, reject) => {
                    api.get(`/online-contents?page=${page}&size=${size}`).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 读取配置
            getSetting() {
                return new Promise((resolve, reject) => {
                    api.get(`/setting`).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            },
            // 写入配置
            addSetting(data) {
                return new Promise((resolve, reject) => {
                    api.post(`/setting`, data).then(res => {resolve(res)}).catch( error => {reject(error)})
                })
            }
        }
    }
)

export default useFileStore
